﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using DataAccess.DB;

namespace DuocThaiBinh.Controllers
{
    public class KhoController : BaseController
    {
        private db_YTeTBEntities db = new db_YTeTBEntities();

        //
        // GET: /Kho/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Administrator")]
        public JsonResult IndexGet()
        {
            int currentCuaHangID = GetCurrentCuaHangID();
            var khoes = db.Khoes.OrderBy(p => p.MaKho).Where(p => p.MaKho != null && p.CuaHangId == currentCuaHangID && p.Status).ToList();

            return Json(new
            {
                data = khoes.Select(x => new { MaKho = x.MaKho, TenKho = x.TenKho, ID = x.Id })
            }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Kho/Create
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Kho/Create
        [Authorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Create(Kho kho)
        {
            if (ModelState.IsValid)
            {
                kho.CuaHangId = Convert.ToInt32(Session["CuaHangID"]);
                kho.Status = true;
                db.Khoes.Add(kho);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(kho);
        }

        //
        // GET: /Kho/Edit/5
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id = 0)
        {
            Kho kho = db.Khoes.Find(id);
            if (kho == null)
            {
                return HttpNotFound();
            }

            if (kho.CuaHangId != GetCurrentCuaHangID())
            {
                return HttpNotFound();
            }

            return View(kho);
        }

        //
        // POST: /Kho/Edit/5
        [Authorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Edit(Kho kho)
        {
            if (kho.CuaHangId != GetCurrentCuaHangID())
            {
                return RedirectToAction("Index");
            }

            if (ModelState.IsValid)
            {
                db.Entry(kho).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(kho);
        }

        //
        // POST: /Kho/Delete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult Delete(int id)
        {
            Kho kho = db.Khoes.Find(id);
            if (kho.CuaHangId != GetCurrentCuaHangID())
            {
                return Json(new { msg = "failed" });
            }

            kho.Status = false;
            db.Entry(kho).State = EntityState.Modified;
            db.SaveChanges();
            return Json(new { msg = "success" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}